package org.appfuse.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.TrainingDAO;
import org.appfuse.model.Training;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Training.
 * @see temp.Training
 * @author MyEclipse - Hibernate Tools
 */
public class TrainingDAOHibernate  extends BaseDaoHibernate implements TrainingDAO  {

    private static final Log log = LogFactory.getLog(TrainingDAOHibernate.class);

	//property constants
	public static final String USERID = "userid";
	public static final String PXLB = "pxlb";
	public static final String ZBDW = "zbdw";
	public static final String CXDW = "cxdw";
	public static final String CXDWLB = "cxdwlb";
	public static final String CXDWSZDQ = "cxdwszdq";
	public static final String PXBMC = "pxbmc";
	public static final String PXBLB = "pxblb";
	public static final String ZYMC = "zymc";
	public static final String LGZT = "lgzt";
	public static final String WCQK = "wcqk";

    
	public void save(Training transientInstance) {
		log.debug("saving Training instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(Training transientInstance) {
		log.debug("saving Training instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().update(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

    
	public void delete(Training persistentInstance) {
        log.debug("deleting Training instance");
        try {
        	getSession().setFlushMode(FlushMode.AUTO);
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    

	public Training findById(String a00, Integer id) {
		log.debug("getting Training instance with id: " + id);
		try {
			String queryString = "from Training as model where model.basicInfo.id"
				 + "= ? " + " and model.id = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			queryObject.setParameter(1, id);
			return (Training) queryObject.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	

	public Integer newId(String a00){
		log.debug("generate a new id");
		try {
			String queryString = "select max(id) from Training as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		Integer maxId = (Integer) queryObject.uniqueResult();
		if (maxId != null){
			maxId = maxId + 1;
		}else{
			maxId = 1;
		}
		log.debug("generate new id successful");
		return maxId;
			
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByA00(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = " from Training as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		return queryObject.list();
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}
    public List findByExample(Training instance) {
        log.debug("finding Training instance by example");
        try {
            List results = getSession()
                    .createCriteria("temp.Training")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Training instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Training as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}
	
	public List findByPxlb(Object pxlb) {
		return findByProperty(PXLB, pxlb);
	}
	
	public List findByZbdw(Object zbdw) {
		return findByProperty(ZBDW, zbdw);
	}
	
	public List findByCxdw(Object cxdw) {
		return findByProperty(CXDW, cxdw);
	}
	
	public List findByCxdwlb(Object cxdwlb) {
		return findByProperty(CXDWLB, cxdwlb);
	}
	
	public List findByCxdwszdq(Object cxdwszdq) {
		return findByProperty(CXDWSZDQ, cxdwszdq);
	}
	
	public List findByPxbmc(Object pxbmc) {
		return findByProperty(PXBMC, pxbmc);
	}
	
	public List findByPxblb(Object pxblb) {
		return findByProperty(PXBLB, pxblb);
	}
	
	public List findByZymc(Object zymc) {
		return findByProperty(ZYMC, zymc);
	}
	
	public List findByLgzt(Object lgzt) {
		return findByProperty(LGZT, lgzt);
	}
	
	public List findByWcqk(Object wcqk) {
		return findByProperty(WCQK, wcqk);
	}
	
    public Training merge(Training detachedInstance) {
        log.debug("merging Training instance");
        try {
            Training result = (Training) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Training instance) {
        log.debug("attaching dirty Training instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Training instance) {
        log.debug("attaching clean Training instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}